package com.flower8.controller;

import com.flower8.dto.*;
import com.flower8.model.User;
import com.flower8.service.UserService;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户信息控制层
 * @Author: tanglin
 * @Date: 2019/09/09 15:31
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    /**
     * 查询所有用户列表
     * @param request
     * @return
     */
    @RequestMapping("/getUserList")
    public Object getAdminList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询所有用户列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<UserDto> data =  userService.getUserList(pd);
            int total = userService.getUserListCount(pd);
            res.put("data", data);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @RequestMapping("/getUserById")
    public Object getUserById(@RequestParam long id){
        logBefore(log, "++++++++++ 根据用户id："+id+"查询用户信息 ++++++++++");

        Map res = new HashMap();
        try {
            User user = userService.getUserById(id);
            res.put("user", user);
            logAfter(log);
            return responseResultForSuccess(res);
        } catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 根据用户id删除用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteUser")
    public Object deleteById(@RequestParam long id){
        logBefore(log, "++++++++++ 根据用户id删除用户 ++++++++++");
        try {
            userService.deleteById(id);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    public Object updateUser(@RequestBody User user){
        logBefore(log, "++++++++++ 更新用户信息 ++++++++++");
        try {
            userService.updateUserById(user);
            logAfter(log);
            return responseResultForSuccess(null, "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("修改失败");
        }
    }


    /**
     * 查询用户积分记录
     * @param request
     * @return
     */
    @RequestMapping("/getUserPoints")
    public Object getUserPoints(HttpServletRequest request){
        logBefore(log, "++++++++++  查询用户积分记录 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );
        String endDate = pd.get("endDate").toString();
        if (endDate != null && endDate.trim().length() > 0) {
            endDate = endDate + " 23:59:59";
            pd.put("endDate", endDate);
        }

        Map res = new HashMap();
        try {
            List<UserPointsDto> list =  userService.getUserPoints(pd);
            int total = userService.getUserPointsCount(pd);
            res.put("data", list);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询用户总积分
     * @param request
     * @return
     */
    @RequestMapping("/getTotPointByUser")
    public Object getTotPointByUser(HttpServletRequest request) {
        logBefore(log, "++++++++++  查询用户总积分 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            int total = userService.getTotPointByUser(pd);
            logAfter(log);
            res.put("total", total);
            return responseResultForSuccess(res);
        } catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("修改失败");
        }
    }

    /**
     * 查询用户信用分记录
     * @param request
     * @return
     */
    @RequestMapping("/getUserCreditPoints")
    public Object getUserCreditPoints(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询用户信用分记录 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<UserCreditPointDto> list =  userService.getUserCreditPoints(pd);
            int total = userService.getUserCreditPointsCount(pd);
            res.put("data", list);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询用户总信用分
     * @param request
     * @return
     */
    @RequestMapping("/getTotalCreditPoint")
    public Object getTotalCreditPoint(HttpServletRequest request) {
        logBefore(log, "++++++++++  查询用户总积分 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
	        int total = userService.getTotalCreditPoint(pd);
	        logAfter(log);
            res.put("total", total);
            return responseResultForSuccess(res);
        } catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("修改失败");
        }
    }


    /**
     * 查询需要人工审核的用户
     * @param request
     * @return
     */
    @RequestMapping("/getNotPass")
    public Object getNotPassUser(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询需要人工审核的用户 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<UserDto> list =  userService.getNotPassUser(pd);
            long total = userService.getNotPassUserCount(pd);
            res.put("data", list);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 审核用户实名认证信息
     * @param request
     * @return
     */
    @RequestMapping("/checkUser")
    public Object checkUser(HttpServletRequest request) {
        logBefore(log, "++++++++++  审核用户实名认证信息 ++++++++++");
        PageData pd = new PageData(request);
        try {
            userService.checkUser(pd);
            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }

    /**
     * 查询所有用户注销申请列表
     * @param request
     * @return
     */
    @RequestMapping("/getUserCancelationApplicationList")
    public Object getUserCancelationApplicationList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询所有用户列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<UserCancelationApplicationDto> data =  userService.getUserCancelationApplicationList(pd);
            int total = userService.getUserCancelationApplicationListCount(pd);
            res.put("data", data);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 受理用户注销
     * @param id
     * @return
     */
    @RequestMapping("/acceptCancelationApplication")
    public Object acceptCancelationApplication(@RequestParam long id){
        logBefore(log, "++++++++++ 受理用户注销 ++++++++++");
        try {
            userService.acceptCancelationApplication(id);
            logAfter(log);
            return responseResultForSuccess(null, "注销成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 驳回用户注销
     * @param id
     * @return
     */
    @RequestMapping("/rejectCancelationApplication")
    public Object rejectCancelationApplication(@RequestParam long id){
        logBefore(log, "++++++++++ 驳回用户注销 ++++++++++");
        try {
            userService.rejectCancelationApplication(id);
            logAfter(log);
            return responseResultForSuccess(null, "驳回成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询用户总可用额度
     * @param request
     * @return
     */
    @RequestMapping("/getTotalUsableAmt")
    public Object getTotalUsableAmt(HttpServletRequest request) {
        logBefore(log, "++++++++++  查询用户总可用额度 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            res = userService.getTotalUsableAmt(pd);
            logAfter(log);
            return responseResultForSuccess(res);
        } catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("修改失败");
        }
    }

    /**
     * 查询用户可用额度记录
     * @param request
     * @return
     */
    @RequestMapping("/getUserUsableAmtList")
    public Object getUserUsableAmtList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询用户可用额度记录 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<Map> list =  userService.getUserUsableAmtList(pd);
            int total = userService.getUserUsableAmtCount(pd);
            res.put("data", list);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

}
